<template>
<div class="login_container">
    <div class="login_box">
        <el-form label-width="0px" class="login_form" :model="loginForm" :rules="rules" ref="loginForm">
            <el-form-item prop="workNumber">
                <el-input placeholder="请输入工号" prefix-icon="el-icon-user" v-model="loginForm.workNumber"></el-input>
            </el-form-item>
            <el-form-item prop="password" @keyup.enter.native="login">
                <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" show-password v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item class="btns">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="warning" @click="reset_form">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                workNumber: '',
                password: '',
            },
            rules: {
              workNumber: [{
                        required: true,
                        message: '不得为空',
                        trigger: 'blur',
                    },
                    {
                        min: 3,
                        max: 10,
                        message: '长度要大于3小于10',
                        trigger: 'blur',
                    },
                ],
                password: [{
                    required: true,
                    message: '不得为空',
                    trigger: 'blur',
                }, ],
            },
        }
    },
    methods: {
        reset_form: function () {
            this.$refs.loginForm.resetFields()
        },
        login: function () {
            const _this = this
            this.$refs.loginForm.validate(async (valid) => {
                if (!valid) {
                    return
                }
                const {
                    data: result
                } = await this.$http.post('/user/login',this.loginForm)
                console.log(result)
                this.$message.success('登陆成功!')
                window.sessionStorage.setItem('token', result.data.token)
                window.sessionStorage.setItem('isHr', result.data.isHr)
                await this.$router.push('/welcome')
            })
        },
    },
}
</script>

<style lang="less" scoped>
.login_container {
    height: 100%;
    background-color: #2b4b6b;
}

.login_box {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.avatar_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 20px rgb(255, 239, 15);
    background-color: rgb(255, 239, 11);

    img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
    }
}

.login_form {
    position: absolute;
    bottom: 20px;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
}

.btns {
    display: flex;
    // 尾部对齐
    justify-content: flex-end;
}
</style>
